Developer Documentation
PATH  Mac OS X Documentation > Developer Tools > The Objective-C Compiler


Previous | Contents | Next

Function Prototyping

Function prototypes are a new and important feature of the ANSI standard. You should use function prototypes in your C programs, so the compiler can generate more efficient code (because it knows what the called function is expecting). The compiler can also warn you when you pass the wrong number or wrong type of arguments to a function.

Extra care must be taken in using function prototypes. Be sure to follow these rules:

Here are a few points about prototyping that might cause you some trouble.

int foo (short); int foo (x) short x; { . . . }

The error message is correct. The code is wrong because the old-style nonprototype definition passes subword integers in their promoted types. In other words, the argument is really an int , not a short . The correct prototype is this:

int foo (int) int foo (struct mumble *); struct mumble { . . . }; int foo (struct mumble *x); { . . . }

This code is also wrong. Because of the scope of struct mumble , the prototype is limited to the argument list containing it. It doesn't refer to the struct mumble defined with file scope immediately below--they are two unrelated types with similar names in different scopes. But in the definition of foo , the file-scope type is used because that is available to be inherited. Thus, the definition and the prototype don't match and you get an error. You can make the code work by simply moving the definition of struct mumble above the prototype.

"Suggested Reading" lists several C books that provide detailed information about the use (and abuse) of function prototypes.


The Objective-C Compiler

Previous | Contents | Next